import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @ClassName KP17_linkListToStack
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/10/12 17:51
 */
public class KP17_linkListToStack {

    public static class Node<V> {
        public V value;
        public Node next;
        public Node(V value) {
            this.value = value;
        }
    }


    public static class MyStack<V> {
        private Node<V> head;
        private int size;

        public MyStack() {
            this.head = null;
            this.size = 0;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public int size() {
            return size;
        }

        public void push(V value) {
            Node<V> node = new Node<>(value);
            if (head == null) {
                head = node;
            } else {
                node.next = head;
                head = node;
            }
            size++;
        }

        public V pop() {
            V del = null;
            if (head != null) {
                del = head.value;
                head = head.next;
                size--;
            }
            return del;
        }

        public V peek() {
            return head == null ? null : head.value;

           /* V top = null;
            if (head != null) {
                top = head.value;
            }
            return top;*/
        }
    }

    public static void testStack() {
        System.out.println("测试开始");
        MyStack<Integer> myStack = new MyStack<>();
        Stack<Integer> testStack = new Stack<>();
        int testTimes = 100;
        int maxValue = 1000;
        for (int i = 0; i < testTimes; i++) {
            if (myStack.isEmpty() != testStack.isEmpty()) {
                System.out.println("error1");
            }
            if (myStack.size() != testStack.size()) {
                System.out.println("error2");
            }
            double decide = Math.random();
            if (decide < 0.33) {
                int rand = (int) (Math.random() * (maxValue + 1));
                myStack.push(rand);
                testStack.push(rand);
            } else if (decide < 0.66) {
                if (!myStack.isEmpty()) {
                    int poll1 = myStack.pop();
                    int poll2 = testStack.pop();
                    if (poll1 != poll2) {
                        System.out.println("error3");
                    }
                }
            } else {
                if (!myStack.isEmpty()) {
                    int poll1 = myStack.peek();
                    int poll2 = testStack.peek();
                    if (poll1 != poll2) {
                        System.out.println("error4");
                    }
                }
            }
        }
        if (myStack.size() != testStack.size()) {
            System.out.println("error5");
        }
        while (!myStack.isEmpty()) {
            int poll1 = myStack.pop();
            int poll2 = testStack.pop();
            if (poll1 != poll2) {
                System.out.println("error6");
            }
        }
        System.out.println("测试结束！");
    }

    public static void main(String[] args) {
        testStack();
    }
}
